<?php namespace Summer\Hub\ReportWidgets;

use Backend\Classes\ReportWidgetBase;
use BackendAuth;
use Exception;
use Illuminate\Support\Facades\DB;
use Flash;
use Input;
use Validator;
use ValidationException;
use ApplicationException;

class DataInit extends ReportWidgetBase
{
    public function render()
    {
        try {
            $this->loadData();
        }
        catch (Exception $ex) {
            $this->vars['error'] = $ex->getMessage();
        }



        return $this->makePartial('widget');
    }

    public function defineProperties()
    {
        return [
            'title' => [
                'title'             => '数据初始化',
                'default'           => '数据初始化',
                'type'              => 'string',
                'validationPattern' => '^.+$',
                'validationMessage' => '数据初始化错误'
            ]
        ];
    }

    public function onDataInit()
    {
        $rules = [
            'type' => 'required',
            'initpwd' => 'required',
        ];
        $messages = [
            'type.required'    => '请选择初始类型.',
            'initpwd.required'    => '密码不能为空.',
        ];
        $validation = Validator::make(post(), $rules,$messages);
        if ($validation->fails()) {
            throw new ValidationException($validation);
        }
        try {
            $initpwd=Input::get('initpwd');
            $user = BackendAuth::getUser();
            if (!$user->checkHashValue('password', $initpwd)) {
                throw new ApplicationException("密码错误");
            }
            $type=Input::get('type');
            if($type=='user'){
                $this->initUser();
            }elseif($type=='other'){
                $table=Input::get('table');
                if(empty($table)){
                    throw new ApplicationException("请选择表类型");
                }
                foreach ($table as $tv){
                    if($tv=='detailed'){
                        $this->initDetailed();
                    }
                    if($tv=='record'){
                        $this->initRecord();
                    }
                    if($tv=='base'){
                        $this->initBase();
                    }
                }
            }elseif($type=='project'){
                $this->initProject();
            }elseif($type=='box'){
                $this->initBox();
            }


            Flash::success('数据初始化成功!');
        }catch (Exception $ex) {
            Flash::error($ex->getMessage());
        }

    }
    protected function loadData()
    {
    }
    /**
     * 初始化用户数据【初始化用户信息一定包含基础信息】【只初始化化基础信息保留用户信息，包括用户表，钱包表】
     */
    protected function initUser()
    {
        DB::table('users')->truncate();//用户表
        DB::table('summer_hjd_wallet')->truncate();//钱包表
        $this->initDetailed();
        $this->initRecord();
        $this->initBase();
    }
    //财务流水表
    protected function initDetailed()
    {
        DB::table('summer_hjd_detailed_bonus')->truncate();
        DB::table('summer_hjd_detailed_cash')->truncate();
        DB::table('summer_hjd_detailed_score')->truncate();
        //项目扩展的其他币种
        DB::table('summer_hjd_detailed_heart')->truncate();
        DB::table('summer_hjd_detailed_stock')->truncate();
    }
    //记录表
    protected function initRecord()
    {
        DB::table('summer_hjd_bonus_record')->truncate();//奖金记录表
        DB::table('summer_hjd_finance')->truncate();//财务表
        DB::table('summer_hjd_rg_record')->truncate();//充值记录表
        DB::table('summer_hjd_tf_record')->truncate();//转账记录表
        DB::table('summer_hjd_wd_record')->truncate();//提现记录表
    }
    /**
     * 初始化基础数据【信息表，地址表】
     */
    protected function initBase()
    {
        //公告站内信
        DB::table('summer_hjd_message')->truncate();
        //用户地址表
        DB::table('summer_location_user_address')->truncate();
    }
    /**
     * 初始化该项目对应数据
     */
    protected function initProject()
    {
    }
    protected function initBox()
    {
        //盲盒表
        DB::table('summer_box_items')->truncate();
        //盲盒订单表
        DB::table('summer_box_orders')->truncate();
        //盲盒业绩记录表
        DB::table('summer_box_yeji')->truncate();
    }

}
